package dao.impl;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import dao.OrderDAO;
import dao.OrderGoodsDAO;
import po.Goods;
import po.Order;
import po.OrderGoods;
import po.Student;

@Repository("od")
public class OrderDAOImpl implements OrderDAO {
	@PersistenceContext(name="un")
	private EntityManager em;

	@Override
	public void addOrders(Order order) {
		// TODO Auto-generated method stub
		
		System.out.println("add order");
		em.persist(order);
		System.out.println("persist");

	}

	@Override
	public void delOrders(int ido) {
		// TODO Auto-generated method stub

	}



	@Override
	public List<Order> listOrdersByCid(String cid) {
		// TODO Auto-generated method stub
		System.out.println("list orders");
		String jpql="select s from Order s where s.cid=:cid";
		List<Order> ls=em.createQuery(jpql)
				.setParameter("cid", cid)
				.getResultList();
		System.out.println("list over!");
		System.out.println(ls==null);
		if(ls.isEmpty()) return null;
		else return ls;

	}

	@Override
	public List<Order> listOrdersByBid(String bid) {
		// TODO Auto-generated method stub
		String jpql="select s from Order s where s.bid=:bid";
		List<Order> ls=em.createQuery(jpql)
				.setParameter("bid", bid)
				.getResultList();
		if(ls.isEmpty()) return null;
		else return ls;
	}

	@Override
	public int updateOrderStatus(String orderId, int type) {
		// TODO Auto-generated method stub
		String jpql="update Order s set s.status=:type  where s.bid=:orderId";
		em.createQuery(jpql)
			.setParameter("type", type)
			.setParameter("orderId", orderId);
		return 0;
	}

}
